using System;
using System.Text;
using System.Data;

namespace Antu.Core.Ajax
{
    /// <summary>
    /// Provides methods to serialize and deserialize a DataView object.
    /// </summary>
    public class DataViewConverter : AbstractJavascriptConverter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataViewConverter"/> class.
        /// </summary>
        public DataViewConverter()
            : base()
        {
            allowInheritance = true;

            serializabletypeslist = new Type[] { typeof( DataView ) };
        }

        /// <summary>
        /// Converts a .NET object into a JSON string.
        /// </summary>
        /// <param name="o">The object to convert.</param>
        /// <returns>Returns a JSON string.</returns>
        public override string Serialize( object o )
        {
            StringBuilder sb = new StringBuilder();
            Serialize( o , sb );
            return sb.ToString();
        }

        /// <summary>
        /// Serializes the specified o.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="sb">The sb.</param>
        public override void Serialize( object o , StringBuilder sb )
        {
            DataView dv = o as DataView;

            if ( dv == null )
                throw new NotSupportedException();

            DataTableConverter dtc = new DataTableConverter();
            dtc.Serialize( dv.ToTable() , sb );
        }
    }
}
